<template>
  <div class="star" :class="starSize">
    <span class="star-item" v-for="className in itemClass" :class="className"></span>
  </div>
</template>

<script type="text/ecmascript-6">
  const ON = 'on';
  const HALF = 'half';
  const OFF = 'off';
  const TOTAL = 5;

  export default{
    name:'star',
    props:{
      score:{
        type: Number
      },
      size:{
        type: Number
      }
    },
    data() {
      return{

      }
    },
    computed:{
      starSize() {
        return 'star-'+this.size;
      },
      itemClass() {
        let result = [];
        let starScore = this.score;
        starScore = Math.floor(starScore * 2) / 2;
        let hasDecimal = starScore % 1 !== 0
        starScore = Math.floor(starScore);
        for (let i = 0; i < starScore; i++){
          result.push(ON);
        }
        if (hasDecimal){
          result.push(HALF);
        }
        while (result.length < TOTAL){
          result.push(OFF);
        }
        return result;
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss">
@import "../../common/mixin.scss";
  .star{
    display: inline-block;
    .star-item{
      display: inline-block;
      &:last-child{
        margin-right: 0;
      }
    }
    &.star-48{
      .star-item{
        width: 20px;
        height: 20px;
        background-size: 20px 20px;
        margin-right: 22px;
      }
      .star-item.on{
        @include bg_image('star48_on');
      }
      .star-item.half{
        @include bg_image('star48_half');
      }
      .star-item.off{
        @include bg_image('star48_off');
      }
    }
    &.star-36{
      .star-item{
        width: 15px;
        height: 15px;
        background-size: 15px 15px;
        margin-right: 6px;
      }
      .star-item.on{
        @include bg_image('star36_on');
      }
      .star-item.half{
        @include bg_image('star36_half');
      }
      .star-item.off{
        @include bg_image('star36_off');
      }
    }
    &.star-24{
      .star-item{
        width: 10px;
        height: 10px;
        background-size: 10px 10px;
        margin-right: 3px;
      }
      .star-item.on{
        @include bg_image('star24_on');
      }
      .star-item.half{
        @include bg_image('star24_half');
      }
      .star-item.off{
        @include bg_image('star24_off');
      }
    }

  }

</style>
